package com.wsoft.controller;

import com.wsoft.core.utils.ExcelUtil;
import com.wsoft.core.utils.FormExcelUtil;
import com.wsoft.core.utils.WordUtil;
import com.wsoft.excel.ExcelTestUser;
import com.wsoft.excel.config.ExcelRangeConfig;
import com.wsoft.excel.config.ExcelStyleConfig;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @Author z001
 * @Date 2024/11/5 10:21
 */
@RestController
@RequestMapping("/excel")
public class ExcelTestController {

    @Operation(summary = "测试导出Excel", description = "测试导出Excel")
    @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<ExcelTestUser> users = new ArrayList<>();

        users.add(new ExcelTestUser("张三",new Date(), LocalDateTime.now(),10000.0, "001","10","1","2226666","444"));
        users.add(new ExcelTestUser("王五",new Date(), LocalDateTime.now(),10000.00, "001","11","2","333","555"));
        users.add(new ExcelTestUser("李四",new Date(), LocalDateTime.now(),12000.02, "001001","12","1845766264479703042","122266669999","666"));


        //指定返回 字段
        Set<String> columnMappings = new HashSet<>();
//        columnMappings.add("name");
//        columnMappings.add("gender");

        ExcelStyleConfig headStyle = new ExcelStyleConfig();
        headStyle.setFontColor(IndexedColors.RED);
        headStyle.setBackgroundColor(IndexedColors.GOLD);

        List<ExcelRangeConfig> configList = new ArrayList<>();
        //直接导出
//        ExcelUtil.exportExcel(response,"user.xlsx",null,null, ExcelTestUser.class,columnMappings,headStyle,null,configList,null);

        //直接导出到指定路径的
        ExcelUtil.exportExcel("/Users/leo/Desktop/file/user.xlsx",null,users,ExcelTestUser.class,columnMappings,headStyle,null,configList,null);

     /*   //返回输出流
        ByteArrayOutputStream outputStream = ExcelUtil.exportExcel(null, users, ExcelTestUser.class, columnMappings, headStyle, null, configList, null);
        // 文件保存路径
        String filePath = "/Users/leo/Desktop/file/user.xlsx";
        try (FileOutputStream fos = new FileOutputStream(filePath)) {
            fos.write(outputStream.toByteArray());
            System.out.println("文件已成功保存到: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
*/

        LinkedHashMap<String, List<ExcelTestUser>> dataMap  = new LinkedHashMap<>(16);
        dataMap.put("sheet1", users);
        dataMap.put("sheet2", users);

        LinkedList<String> sheetNameList = new LinkedList<>();
        sheetNameList.add("sheet1");
        sheetNameList.add("sheet3");

        LinkedList<List<ExcelTestUser>> dataList = new LinkedList<>();
        dataList.add(users);
        dataList.add(users);


//        ExcelUtil.exportMultiSheet(response,sheetNameList);

        //主表
//        FormExcelUtil.exportFormTemplate(response,"text2.xlsx",1837735237254152201L,null,null,null);

        //子表
//        ExcelUtil.exportFormTemplate(response,"text2.xlsx",1837735237254152202L,null);

    }


    @Operation(summary = "测试导出Excel2", description = "测试导出Excel")
    @PostMapping("/exportExcel2")
    public void exportExcel2(HttpServletResponse response) throws IOException {

        /*List<List<String>> headList = new ArrayList<>();
        headList.add(Arrays.asList("标题"));
        headList.add(Arrays.asList("内容"));

        List<List<String>> dataList = new ArrayList<>();
        dataList.add(Arrays.asList("标题1","内容1"));
        dataList.add(Arrays.asList("标题2","内容2"));
        dataList.add(Arrays.asList("标题3","内容4"));
        FormExcelUtil.exportFormData(response,"text2.xlsx","sheet1",headList,dataList);*/

        //主表导出模板
//        FormExcelUtil.exportFormTemplate(response,"article.xlsx",1L,null,null,null);
        //子表导出模板
        FormExcelUtil.exportFormTemplate(response,"articleComment.xlsx",2L,1L,null,null);

    }



    @Operation(summary = "导入", description = "导入")
    @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile multipartFile) throws Exception {
//        List<ExcelTestUser> users = ExcelUtil.importExcel(multipartFile.getInputStream(), ExcelTestUser.class);
//        System.out.println(users);
        Long designId = 1L;
        List<Map<Integer, Map<String, Object>>> integerMapMap = FormExcelUtil.importFormExcel(designId,multipartFile.getInputStream());
        for (Map<Integer, Map<String, Object>> mapMap : integerMapMap) {
//            System.out.println(mapMap);
        }

    }

    @Operation(summary = "导出数据",description = "导出数据")
    @PostMapping("/testWord")
    public void testWord(HttpServletResponse response) throws Exception {
        String templatePath = "/Users/leo/Desktop/file/wordTemplate2.docx";
        String outputPath = "/Users/leo/Desktop/output2.docx";
        // 创建一个 Map 来存储需要填充的数据
        Map<String, Object> dataMap = new HashMap<>(16);

        // 静态常量
        dataMap.put("staticValue", "这是一个静态常量");
        // 动态传参
        dataMap.put("dynamicParam", "这是一个动态传参");

        dataMap.put("date","2024-12-02");

        WordUtil.processTemplate(templatePath,outputPath,dataMap);

    }


}
